package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.MovementsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private MovementsApi movementsApi;
    @DubboReference
    private VideoApi videoApi;

    //分页查询用户信息
    public PageResult users(Integer page, Integer pagesize) {
        //1.调用UserInfoApi查询
        IPage ipage = userInfoApi.findByPage(page, pagesize);
        //2.构建返回对象
        PageResult vo = new PageResult(page, pagesize, ipage.getTotal(), ipage.getRecords());
        //3.返回参数
        return vo;
    }

    //查询后台的用户基本信息
    public UserInfo findById(Long userID) {
        UserInfo userInfo = userInfoApi.findById(userID);
        return userInfo;
    }

    //后台需求:根据id查询用户动态
    public PageResult messages(Integer page, Integer pagesize, Long uid, Integer status) {
        //1.调用API查询动态列表，PageResult
        PageResult pageResult = movementsApi.findByPage(page, pagesize, uid, status);
        //2.获取动态列表
        List<Movement> items = (List<Movement>) pageResult.getItems();
        //3.判断动态列表是否存在
        List<MovementsVo> vos = new ArrayList<>();
        if (CollUtil.isEmpty(items)) {
            return pageResult;
        }
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds);
        //4.一个Movement转化一个MovementVo
        for (Movement item : items) {
            UserInfo info = map.get(item.getUserId());
            MovementsVo vo = MovementsVo.init(info, item);
            vos.add(vo);
        }
        //5.构造返回
        pageResult.setItems(vos);
        return pageResult;
    }

    //后台需求：根据id查询用户视频
    public PageResult videos(Integer page, Integer pagesize, Long userId) {
        //1.根据id查询视频分页列表
        PageResult result=videoApi.findByPage(page,pagesize,userId);
        //2.获取到视频列表
        List<Video> items = (List<Video>)result.getItems();
        //3.判断列表是否存在
        if (CollUtil.isEmpty(items)){
            //如果为空，就返回原本包含空集合的PageResult
            return result;
        }
        //4.如果不为空，就要获取到id集合
        List<Long> userIds = CollUtil.getFieldValues(items, "userId",Long.class);
        //5.根据id集合获取到UserInfo对象集合
        Map<Long, UserInfo> infos = userInfoApi.findByIds(userIds);
        //6.循环构造
            //先准备一个返回前端的对象集合
         List<VideoVo> vos = new ArrayList<>();
        for (Video item : items) {
            //根基items的用户id获取到对应的userInfo
            UserInfo info = infos.get(item.getUserId());
            VideoVo vo = VideoVo.init(info, item);
            vos.add(vo);
        }
        //7.构建返回
         result.setItems(vos);
        return result;
    }
}
